package com.itcam.phaseii.generic;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * 定义Employee 类<p>
 * 1) 该类包含：private 成员变量name,sal,birthday，其中birthday 为MyDate 类的对象；<p>
 * 2) 为每一个属性定义getter, setter 方法；<p>
 * 3) 重写toString 方法输出name, sal, birthday<p>
 * 4) MyDate 类包含: private 成员变量month,day,year；并为每一个属性定义getter, setter 方法；<p>
 * 5) 创建该类的3 个对象，并把这些对象放入ArrayList 集合中（ArrayList 需使用泛型来定义），对集合中的元素进行排序，并遍历输出：<p>
 * <p>
 * 排序方式： 调用ArrayList 的sort 方法,<p>
 * 传入Comparator 对象[使用泛型]，先按照name 排序，如果name 相同，则按生日日期的先后排序。【即：定制排序】<p>
 * 有一定难度15min , 比较经典泛型使用案例GenericExercise02.java<p>
 */

@SuppressWarnings({"all"}) //用于告诉编译器忽略特定的警告信息。
public class GenericExercise02 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("tom", 20000, new MyDate(1980, 12, 11)));
        employees.add(new Employee("jack", 12000, new MyDate(2001, 12, 12)));
        employees.add(new Employee("tom", 50000, new MyDate(1980, 12, 10)));

        System.out.println("employees=" + employees);

        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee em1, Employee em2) {
                // 先按照name排序，如果name相同，则按生日日期的先后排序。
                if (!(em1 instanceof Employee && em2 instanceof Employee)) {
                    System.out.println("类型错误...");
                    return 0;
                }
                // 比较name
                if (em1.getName().compareTo(em2.getName()) != 0) {
                    return em1.getName().compareTo(em2.getName());
                }

                /**
                 * compareTo方法的返回值具体表示如下：
                 *     返回值为0：如果当前对象与另一个对象相等。
                 *     返回值为负整数：如果当前对象小于另一个对象。
                 *     返回值为正整数：如果当前对象大于另一个对象。
                 */

                // 比较MyDate
                return em1.getBirthday().compareTo(em2.getBirthday());
            }
        });

        System.out.println("==对雇员进行排序==");
        System.out.println(employees);
    }
}

class MyDate implements Comparable<MyDate> {
    private Integer year;
    private Integer month;
    private Integer day;

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }


    public MyDate(Integer year, Integer month, Integer day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public Integer getMonth() {
        return month;
    }

    public void setMonth(Integer month) {
        this.month = month;
    }

    public Integer getDay() {
        return day;
    }

    public void setDay(Integer day) {
        this.day = day;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }


    @Override
    public int compareTo(MyDate o) {
        Integer yearMius = year - o.getYear();
        if (yearMius > 0) {
            return yearMius;
        }
        Integer monthMius = month - o.getMonth();
        if (monthMius > 0) {
            return monthMius;
        }
        Integer dayMius = day - o.getDay();
        return dayMius;

    }
}

class Employee {
    private String name;
    private Integer sal;
    private MyDate birthday;

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday='" + birthday + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSal() {
        return sal;
    }

    public void setSal(Integer sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    public Employee(String name, Integer sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }
}
